var express = require('express');
var router = express.Router();
var BaseData = require('.././config/db');
db = new BaseData();
var User_id = "",
	Manage = 0;

/* GET home page. */
router.get('/:User_id/', function(req, res, next) {
	if(req.session.token) {
		var Str = req.session.token;
		User_id = db.Toolaes(Str.toString().split("_")[0], "Decrypt");
	} else {
		User_id = "";
	}
	db.connection.query('SELECT User_id,UserImg,Name,Wob,UserIntro,SubNumber,CentNumber,ColleNumber FROM userinfo WHERE User_id = ?', [req.params.User_id], function(err, rows) {
		if(!err) {
			if(rows[0]) {
				console.log("user_id:" + User_id)
				console.log("req.params.User_id:" + req.params.User_id)
				if(User_id == req.params.User_id) {
					Manage = 1;
				} else {
					Manage = 0;
				}
				res.render('user/index', {
					MyUser_id: User_id,
					User_id: req.params.User_id,
					UserImg: rows[0]['UserImg'],
					Name: rows[0]['Name'],
					Wob: rows[0]['Wob'],
					UserIntro: rows[0]['UserIntro'],
					SubNumber: rows[0]['SubNumber'],
					CentNumber: rows[0]['CentNumber'],
					ColleNumber: rows[0]['ColleNumber'],
					Manage: Manage,
					GetStringConfig: db.GetStringConfig
				});
			} else {
				res.redirect('/');
			}
		}
	})
});

router.post('/', function(req, res, next) {
	var obj = {
		user: {
			User_id: [],
			UserImg: [],
			Name: [],
			Wob: [],
			UserIntro: [],
			SubNumber: [],
			CentNumber: [],
			ColleNumber: []
		},
		sub: {
			Sub_id: [],
			Content: [],
			SubCent: [],
			SubColle: [],
			UpNumber: [],
			BuryNumber: [],
			SubTime: []
		}
	}
	if(req.body.User_id) {
		db.connection.query('select user.User_id,user.UserImg,user.Name,user.Wob,user.UserIntro,user.SubNumber,user.CentNumber,user.ColleNumber,sub.Sub_id,sub.Content,sub.SubCent,sub.SubColle,sub.UpNumber,sub.BuryNumber,sub.SubTime from (select User_id,UserImg,Name,Wob,UserIntro,SubNumber,CentNumber,ColleNumber from userinfo where User_id=?) user inner join subinfo sub on user.User_id=sub.User_id', [req.body.User_id],
			function(err, rows) {
				if(err) {
					res.json({
						"code": "3004",
						"msg": "系统出现错误,请联系管理员"
					})
					return false;
				}
				if(rows.length > 1) {
					for(var i = 0; i < rows.length; i++) {
						obj.user.User_id.push(rows[i]['User_id']);
						obj.user.UserImg.push(rows[i]['UserImg']);
						obj.user.Name.push(rows[i]['Name']);
						obj.user.Wob.push(rows[i]['Wob']);
						obj.user.UserIntro.push(rows[i]['UserIntro']);
						obj.user.SubNumber.push(rows[i]['SubNumber']);
						obj.user.CentNumber.push(rows[i]['CentNumber']);
						obj.user.ColleNumber.push(rows[i]['ColleNumber']);
						obj.sub.Sub_id.push(rows[i]['Sub_id']);
						obj.sub.Content.push(rows[i]['Content']);
						obj.sub.SubCent.push(rows[i]['SubCent']);
						obj.sub.SubColle.push(rows[i]['SubColle']);
						obj.sub.UpNumber.push(rows[i]['UpNumber']);
						obj.sub.BuryNumber.push(rows[i]['BuryNumber']);
						obj.sub.SubTime.push(rows[i]['SubTime']);
					}
					res.json({
						"code": "3000",
						"msg": "success",
						"info": obj
					})
				} else {
					db.connection.query('select User_id,UserImg,Name,Wob,UserIntro,SubNumber,CentNumber,ColleNumber from userinfo where User_id = ?', [req.body.User_id], function(err, rows) {
						if(rows.length > 0) {
							obj.user.User_id.push(rows[0]['User_id']);
							obj.user.UserImg.push(rows[0]['UserImg']);
							obj.user.Name.push(rows[0]['Name']);
							obj.user.Wob.push(rows[0]['Wob']);
							obj.user.UserIntro.push(rows[0]['UserIntro']);
							obj.user.SubNumber.push(rows[0]['SubNumber']);
							obj.user.CentNumber.push(rows[0]['CentNumber']);
							obj.user.ColleNumber.push(rows[0]['ColleNumber']);
							res.json({
								"code": "3000",
								"msg": "success",
								"info": obj
							})
						} else {
							res.json({
								"code": "3004",
								"msg": "用户不存在"
							})
						}
					})
				}
			})
	}
})

router.get('/:User_id/favorites/', function(req, res, next) {
	if(req.session.token) {
		var Str = req.session.token;
		User_id = db.Toolaes(Str.toString().split("_")[0], "Decrypt");
	} else {
		User_id = "";
	}
	db.connection.query('SELECT User_id,UserImg,Name,Wob,UserIntro,SubNumber,CentNumber,ColleNumber FROM userinfo WHERE User_id = ?', [req.params.User_id], function(err, rows) {
		if(!err) {
			if(rows[0]) {
				console.log("user_id:" + User_id)
				console.log("req.params.User_id:" + req.params.User_id)
				if(User_id == req.params.User_id) {
					Manage = 1;
				} else {
					Manage = 0;
				}
				res.render('user/favorites', {
					MyUser_id: User_id,
					User_id: req.params.User_id,
					UserImg: rows[0]['UserImg'],
					Name: rows[0]['Name'],
					Wob: rows[0]['Wob'],
					UserIntro: rows[0]['UserIntro'],
					SubNumber: rows[0]['SubNumber'],
					CentNumber: rows[0]['CentNumber'],
					ColleNumber: rows[0]['ColleNumber'],
					Manage: Manage,
					GetStringConfig: db.GetStringConfig
				});
			} else {
				res.redirect('/');
			}
		}
	})
})

router.post('/favorites', function(req, res, next) {
	var obj = {
		user: {
			User_id: [],
			UserImg: [],
			Name: [],
			Wob: [],
			UserIntro: [],
			SubNumber: [],
			CentNumber: [],
			ColleNumber: []
		},
		sub: {
			Sub_id: [],
			Content: [],
			SubCent: [],
			SubColle: [],
			UpNumber: [],
			BuryNumber: [],
			SubTime: []
		}
	}
	console.log("查询用户:" + req.body.User_id)
	if(req.body.User_id) {
		db.connection.query('select user.User_id,user.UserImg,user.Name,user.Wob,user.UserIntro,user.SubNumber,user.CentNumber,user.ColleNumber,sub.Sub_id,sub.Content,sub.SubCent,sub.SubColle,sub.UpNumber,sub.BuryNumber,sub.SubTime from (select User_id,UserImg,Name,Wob,UserIntro,SubNumber,CentNumber,ColleNumber from userinfo where User_id=(select User_id from subinfo where Sub_id = (select Sub_id from colleinfo where User_id=?))) user left join subinfo sub on sub.Sub_id = (select Sub_id from colleinfo where User_id=?)', [req.body.User_id, req.body.User_id],
			function(err, rows) {
				if(err) {
					res.json({
						"code": "3004",
						"msg": "系统出现错误,请联系管理员"
					})
					return false;
				}
				console.log(rows.length)
				if(rows.length > 0) {
					if(rows[0]['Sub_id']) {
						for(var i = 0; i < rows.length; i++) {
							obj.user.User_id.push(rows[i]['User_id']);
							obj.user.UserImg.push(rows[i]['UserImg']);
							obj.user.Name.push(rows[i]['Name']);
							obj.user.Wob.push(rows[i]['Wob']);
							obj.user.UserIntro.push(rows[i]['UserIntro']);
							obj.user.SubNumber.push(rows[i]['SubNumber']);
							obj.user.CentNumber.push(rows[i]['CentNumber']);
							obj.user.ColleNumber.push(rows[i]['ColleNumber']);
							obj.sub.Sub_id.push(rows[i]['Sub_id']);
							obj.sub.Content.push(rows[i]['Content']);
							obj.sub.SubCent.push(rows[i]['SubCent']);
							obj.sub.SubColle.push(rows[i]['SubColle']);
							obj.sub.UpNumber.push(rows[i]['UpNumber']);
							obj.sub.BuryNumber.push(rows[i]['BuryNumber']);
							obj.sub.SubTime.push(rows[i]['SubTime']);
						}
						res.json({
							"code": "3000",
							"msg": "success",
							"info": obj
						})
					} else {
						obj.user.User_id.push(rows[0]['User_id']);
						obj.user.UserImg.push(rows[0]['UserImg']);
						obj.user.Name.push(rows[0]['Name']);
						obj.user.Wob.push(rows[0]['Wob']);
						obj.user.UserIntro.push(rows[0]['UserIntro']);
						obj.user.SubNumber.push(rows[0]['SubNumber']);
						obj.user.CentNumber.push(rows[0]['CentNumber']);
						obj.user.ColleNumber.push(rows[0]['ColleNumber']);
						res.json({
							"code": "3000",
							"msg": "success",
							"info": obj
						})
					}
				} else {
					res.json({
						"code": "3004",
						"msg": "没有收藏"
					})
				}
			})
	}
})

module.exports = router;